import { Injectable, Inject } from '@angular/core';
import { HttpHeaders, HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
import { User, Project } from '../domain/index';



@Injectable()
export class UserService {
    private readonly domain = 'users';
    private headers = new HttpHeaders({
        'Content-Type': 'application/json'
    });
    constructor(private http: HttpClient,
        @Inject('BASE_CONFIG') private config) {
    }

    searchUsers(filter: string): Observable<User[]> {
        const uri = `${this.config.uri}/${this.domain}`;
        return this.http
            .get<User[]>(uri, { params: { 'eamil_like': filter } });
    }

    getUsersByProject(projectId: string): Observable<User[]> {
        const uri = `${this.config.uri}/${this.domain}`;
        return this.http
            .get<User[]>(uri, { params: { 'projectId': projectId } });
    }

    addProjectRef(user: User, projectId: string): Observable<User> {
        const uri = `${this.config.uri}/${this.domain}/${user.id}`;
        const projectIds = user.projectIds || [];
        if (projectIds.indexOf(projectId) > -1) {
            return Observable.of(user);
        }
        return this.http.patch<User>(uri, JSON.stringify({
            projectIds: [...projectIds, projectId]
        }), { headers: this.headers });
    }

    removeProjectRef(user: User, projectId: string): Observable<User> {
        const uri = `${this.config.uri}/${this.domain}/${user.id}`;
        const projectIds = user.projectIds || [];
        const index = projectIds.indexOf(projectId);
        if (index === -1) {
            return Observable.of(user);
        }
        const toUpdate = [...projectIds.slice(0, index), ...projectIds.slice(index)];
        return this.http.patch<User>(uri, JSON.stringify({
            projectIds: [...projectIds, projectId]
        }), { headers: this.headers });
    }

    batchUpdateProjectRef(project: Project): Observable<User[]> {
        const projectId = project.id;
        const memberIds = project.members || [];
        return Observable.from(memberIds)
            .switchMap(id => {
                const uri = `${this.config.uri}/${this.domain}/${id}`;
                return this.http.get<User>(uri);
            })
            .filter(user => user.projectIds.indexOf(projectId) === -1)
            .switchMap(u => this.addProjectRef(u, projectId))
            .reduce((arrs, curr) => [...arrs, curr], []);
    }
}
